package com.wwl.cms.utils.designModeTest.interpreter;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/9/26 20:37
 * @description:
 */
public class Calculator {
    private Expression expression;

    public Calculator(String expStr) {
        //安排运算先后顺序
        Stack<Expression> stack = new Stack<>();
        //表达式拆分成字符数组
        char[] charArray = expStr.toCharArray();

        Expression left = null;
        Expression right = null;

        for (int i=0;i<charArray.length;i++){
            switch (charArray[i]){
                case '+':
                    left = stack.pop();
                    right = new VarExpression(String.valueOf(charArray[++i]));
                    stack.push(new AddExpression(left,right));
                    break;
                case '-':
                    left = stack.pop();
                    right = new VarExpression(String.valueOf(charArray[++i]));
                    stack.push(new SubExpression(left,right));
                    break;
                default:
                    stack.push(new VarExpression(String.valueOf(charArray[i])));
                    break;
            }
        }
        //当遍历完整个charArray数组后，stack就得到最后Expression
        this.expression=stack.pop();
    }
    public int run (HashMap<String,Integer> var){
        return this.expression.interpreter(var);
    }
}
